# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import hashlib
import itertools
import logging
import os
import re
from openerp import SUPERUSER_ID
import time
from openerp import tools
from openerp.osv import fields,osv

import base64
from urllib import urlencode
from urlparse import urljoin
from openerp.osv.orm import except_orm
from openerp.tools.translate import _

_logger = logging.getLogger(__name__)


class sale_order(osv.osv):
    _inherit = "sale.order"  
    
    def create(self, cr, uid, vals, context=None):
        partner_obj = self.pool.get('res.partner')
        toan = partner_obj.search(cr, SUPERUSER_ID, [('email','=','quanvm00@yahoo.com')],context=context)
        list_partner = partner_obj.search(cr, SUPERUSER_ID, [], context=context)
        if toan:
####
            mess_string = ''
            product_name = ''
            product_qty = ''
            
    ### line
            for line in vals['order_line']:
                product_name = line[2]['name']
                product_qty = line[2]['product_uos_qty']
                price_unit = line[2]['price_unit']
                mess_string += """<p>Product name : %s and Qty: %s , Price Unit: %s.</p> """%(product_name, product_qty, price_unit )                
            partner_name = partner_obj.browse(cr, uid, vals['partner_id']) and partner_obj.browse(cr, SUPERUSER_ID, vals['partner_id']).name
            if context is None:
                context = {}           
            active_model_pool_name = 'sale.order'
            active_model_pool = self.pool.get(active_model_pool_name)
            subtype = 'mail.mt_comment'
            post_values = {
                'subject': 'Partner: %s and Order date: %s'%(partner_name, vals['date_order'] ),
                'body': mess_string,
                'parent_id': toan[0],
                'partner_ids': toan,               
            }     
            ## gui mail               
            msg_id = active_model_pool.message_post(cr, SUPERUSER_ID, [1], type='comment', subtype=subtype, context=context, **post_values)
####


        order =  super(sale_order, self).create(cr, uid, vals, context=context)
        return order  

sale_order()

